package com.itboye.sunsun.adapter;

import java.util.List;

import com.itboye.sunsun.R;
import com.itboye.sunsun.adapter.FenGuanGoodsAdapter.ViewHolder;
import com.itboye.sunsun.application.App;
import com.itboye.sunsun.beans.DianPuTuiJianBean;
import com.itboye.sunsun.beans.DianPuTuiJianBean.GoodsItemBean;
import com.itboye.sunsun.beans.GoodsDetailBean;
import com.itboye.sunsun.shop.ui.GoodDetailActivity;
import com.itboye.sunsun.utils.XImageLoader;

import android.app.Activity;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
/**
 
 * @author 64
 *
 */
public class DianPuTuiJianAdapter extends BaseAdapter {

	private List< GoodsItemBean>dataList;
	private  Activity activity;
	public DianPuTuiJianAdapter(List<GoodsItemBean>dataList, Activity activity) {
		this.dataList=dataList;
		this.activity=activity;
	}

	@Override
	public int getCount() {
		return (dataList.size()+1)/2;
	}

	@Override
	public String getItem(int position) {
		return dataList.get(position).getId();
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView==null) {
			convertView=LayoutInflater.from(App.ctx).inflate(R.layout.item_goods, null);
			holder=new ViewHolder();
			holder.leftImageView=(ImageView) convertView.findViewById(R.id.leftImageView);
			holder.rightImageView=(ImageView) convertView.findViewById(R.id.rightImageView);
			holder.nameLeft=(TextView) convertView.findViewById(R.id.nameLeft);
			holder.priceLeft=(TextView) convertView.findViewById(R.id.priceLeft);
			holder.nameRight=(TextView) convertView.findViewById(R.id.nameRight);
			holder.priceRight=(TextView) convertView.findViewById(R.id.priceRight);
			convertView.setTag(holder);
			
			holder.leftImageView.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					Intent intent=new Intent(activity, GoodDetailActivity.class);
					intent.putExtra("what", (String)v.getTag());
				//	intent.putExtra("guan", parentId );
					activity.startActivity(intent);
				}
			});
			
			holder.rightImageView.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					Intent intent=new Intent(activity, GoodDetailActivity.class);
					intent.putExtra("what", (String)v.getTag());
					
				//	intent.putExtra("guan", parentId );
					activity.startActivity(intent);
				}
			});
		}else{
			holder=(ViewHolder) convertView.getTag();
		}
		
		holder.leftImageView.setTag(getItem(position*2));
		holder.nameLeft.setText(dataList.get(position*2).getName());
		holder.priceLeft.setText("￥"+dataList.get(position*2).getPrice());
		holder.leftImageView.setImageResource(R.drawable.homepage_off);
		XImageLoader.load(dataList.get(position*2).getImg(), holder.leftImageView);
		
		if (position*2+1<dataList.size()) {
			holder.rightImageView.setVisibility(View.VISIBLE);
			holder.priceRight.setVisibility(View.VISIBLE);
			holder.nameRight.setVisibility(View.VISIBLE);
			
			holder.rightImageView.setTag(getItem(position*2+1));
			holder.nameRight.setText(dataList.get(position*2+1).getName());
			holder.priceRight.setText("￥"+dataList.get(position*2+1).getPrice());
			holder.rightImageView.setImageResource(R.drawable.homepage_off);;
			XImageLoader.load(dataList.get(position*2+1).getImg(), holder.rightImageView);
		}else{
			holder.rightImageView.setVisibility(View.INVISIBLE);
			holder.priceRight.setVisibility(View.INVISIBLE);
			holder.nameRight.setVisibility(View.INVISIBLE);
		}
		 
		
		return convertView;
	}

	class ViewHolder {
		ImageView  leftImageView ;
		ImageView rightImageView;
		
		TextView nameLeft;
		TextView priceLeft;
		TextView priceRight;
		TextView nameRight;
		
	}
}
